using UnityEngine;
using UnityEngine.Networking;

public class AirDrop : NetworkBehaviour
{
	public GameObject Parachute;

	public ItemData[] Item;

	public int ItemNum = 3;

	public Vector3 Offset = new Vector3(0f, 0.1f, 0f);

	private bool unpackaged;

	public string PlayerTag = "Player";

	public float DistanceUnpack = 3f;

	private Rigidbody rig;

	public float FloatForce = 10f;

	private void Start()
	{
		rig = GetComponent<Rigidbody>();
	}

	private void OnCollisionEnter(Collision collision)
	{
		if ((bool)Parachute)
		{
			Parachute.SetActive(value: false);
		}
	}

	private void Update()
	{
		if (!base.isServer)
		{
			return;
		}
		if ((bool)Parachute && Parachute.activeSelf && (bool)rig)
		{
			rig.AddForce(Vector3.up * FloatForce);
		}
		if (unpackaged)
		{
			return;
		}
		TargetCollector targetCollector = UnitZ.aiManager.FindTargetTag(PlayerTag);
		if (targetCollector == null || targetCollector.Targets == null)
		{
			return;
		}
		for (int i = 0; i < targetCollector.Targets.Length; i++)
		{
			if (targetCollector.Targets[i] != null && Vector3.Distance(base.transform.position, targetCollector.Targets[i].transform.position) < DistanceUnpack)
			{
				Spawn();
				unpackaged = true;
			}
		}
	}

	private void Spawn()
	{
		if (!base.isServer)
		{
			return;
		}
		for (int i = 0; i < ItemNum; i++)
		{
			ItemData itemData = null;
			Vector3 position = base.transform.position;
			Vector3 localScale = base.transform.localScale;
			int min = -(int)(localScale.x / 2f);
			Vector3 localScale2 = base.transform.localScale;
			float x = Random.Range(min, (int)(localScale2.x / 2f));
			Vector3 localScale3 = base.transform.localScale;
			int min2 = (int)((0f - localScale3.z) / 2f);
			Vector3 localScale4 = base.transform.localScale;
			Vector3 position2 = DetectGround(position + new Vector3(x, 0f, Random.Range(min2, (int)(localScale4.z / 2f))));
			itemData = ((Item.Length <= 0) ? UnitZ.itemManager.ItemsList[Random.Range(0, UnitZ.itemManager.ItemsList.Length)] : Item[Random.Range(0, Item.Length)]);
			if (itemData != null)
			{
				UnitZ.gameNetwork.RequestSpawnItem(itemData.gameObject, itemData.NumTag, itemData.Quantity, position2, Quaternion.identity);
			}
		}
	}

	private Vector3 DetectGround(Vector3 position)
	{
		if (Physics.Raycast(position, -Vector3.up, out RaycastHit hitInfo, 1000f))
		{
			return hitInfo.point + Offset;
		}
		return position;
	}

	private void UNetVersion()
	{
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool result = default(bool);
		return result;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
	}
}
